package net.wy.commons.web;

import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-8-20
 * Time: 下午4:09
 * To change this template use File | Settings | File Templates.
 */
public class ContentNegotiatingViewResolverExt extends ContentNegotiatingViewResolver {
    private List<ViewResolver> viewResolvers;

    public void setViewResolvers(List<ViewResolver> viewResolvers) {
        this.viewResolvers = viewResolvers;
        super.setViewResolvers(viewResolvers);
    }

    public void loadPrefix(String prefix) {
        if (this.viewResolvers != null)
            for (ViewResolver resolver : this.viewResolvers)
                if ((resolver instanceof InternalResourceViewResolver)) {
                    InternalResourceViewResolver resourceViewResolver = (InternalResourceViewResolver) resolver;
                    resourceViewResolver.setPrefix("/" + prefix + "/");
                    setViewResolvers(this.viewResolvers);
                    resourceViewResolver.clearCache();
                    return;
                }
    }
}
